import message from "./message.js";
import store from './store.js';

let baseURL = "http://localhost:7001";

const httpObject = {
	login: function(code) {
		httpObject.get({
			url: "/front-login-code",
			param: {
				code
			}
		}).then(data => {
			store.commit("loginSuccess", data);
		});
	},
	_changeParam: param => {
		param.data = param.param;
		param.url = baseURL+param.url;
		return param;
	},
	put: param => {
		param.method = "PUT";
		return httpObject._request(httpObject._changeParam(param));
	},
	get: param => {
		param.method = "GET";
		return httpObject._request(httpObject._changeParam(param));
	},
	post: param => {
		param.method = "POST";
		return httpObject._request(httpObject._changeParam(param));
	},
	"delete": param => {
		param.method = "DELETE";
		return httpObject._request(httpObject._changeParam(param));
	},

	_request: param => {
		return new Promise((resolve, reject) => {
			if (isCloud) {
				c1.callContainer({
				  path: param.url, // 填入容器的访问路径（云托管-服务列表-路径）
				  // 其余参数同 wx.request
				  data: param.data,
				  method: param.method,
				  withCredentials: true,
				  header: {
					'content-type': 'application/x-www-form-urlencoded',
				  },
				}).then(res => {
					let response = res.data;
					if (response.STATUS === "SUCCESS") {
						resolve(response.DATA);
					} else if (response.STATUS === "FAIL") {
						message.warning(response.MESSAGE);
					} else if (response.STATUS === "LOGIN_FAIL") {
						// uni.login({
						// 	success: function(res) {
						// 		httpObject.login(res.code);
						// 	}
						// });
					} else if (response.STATUS === "BINDING_FAIL") {
						let openId = response.MESSAGE;
						store.commit("saveOpenId", openId)
						uni.navigateTo({
							url: "../login"
						});
					} else if (response.STATUS === "ERROR") {
						message.error(response.MESSAGE);
					} else {
						message.error("系统出现未知错误！");
					}
				});
			} else {
				uni.request({
				    url: param.url,
					method: param.method,
				    data: param.data,
					withCredentials: true,
					header: {
						'content-type': 'application/x-www-form-urlencoded',
					},
					fail: function(f) {
						console.log(f);
					},
					error: function(e) {
						console.log(e);
					},
				    success: function(res) {
				        let response = res.data;
				        if (response.STATUS === "SUCCESS") {
				        	resolve(response.DATA);
				        } else if (response.STATUS === "FAIL") {
				        	message.warning(response.MESSAGE);
				        } else if (response.STATUS === "LOGIN_FAIL") {
				        	uni.login({
				        		success: function(res) {
				        			httpObject.login(res.code);
				        		}
				        	});
				        } else if (response.STATUS === "BINDING_FAIL") {
							let openId = response.MESSAGE;
							store.commit("saveOpenId", openId)
				        	uni.navigateTo({
				        		url: "../login"
				        	});
				        } else if (response.STATUS === "ERROR") {
				        	message.error(response.MESSAGE);
				        } else {
				        	message.error("系统出现未知错误！");
				        }
				    }
				});
			}

		});
	}
}

export default httpObject;